package com.zhengke.controller.hubf;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.util.FileExport;
import com.zhengke.pojo.SysOrder;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.service.hubf.IOrderService;
/**
 * 用户登录的控制层
 * 
 * @author 小黄先森 Archie huang
 *
 */
@Controller
@RequestMapping("/order")
public class IOrderController {
	// 注入IOrderService层的对象
	@Autowired
	IOrderService iorderService;
	/**
	 * 退出界面的方法
	 * 
	 * @return
	 */
	@RequestMapping("/exit.do")
	public String selectOrderAll() {
		return "/login";
	}
	/**
	 * 进入订单列表的首页的方法
	 * 
	 * @return
	 */
	@RequestMapping("/gotoOrderList.do")
	public String querryFirstOrder() {
		return "/orderList";
	}
	/**
	 * 搜索的方法，模糊查询
	 * 
	 * @param orderSn    订单的号码
	 * @param userId     用户的id
	 * @param userName   用户的姓名
	 * @param orderState 订单的状态
	 * @return
	 */
	@RequestMapping("/querryOrder.do")
	@ResponseBody
	public SysOrderResult querryOrder(String orderSn, String userId, String userName, String orderState, int page,
			int limit) {
		System.out.println("----------" + orderSn + "--------------" + userId + "----------------" + userName
				+ "------------------" + "orderState");
		SysOrderResult sysoOrderResult = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("order_sn", orderSn);
			map.put("user_id", userId);
			map.put("user_name", userName);
			map.put("order_state", orderState);

			// 将前台返回的两个数据的参数进行接收，然后返回，如果后面的参数设置成true的话就会自动进行返回
			PageHelper.startPage(page, limit, true);
			// 根据模糊查询的所有的订单的数据
			List<SysOrder> orderList = iorderService.querryOrder(map);
			System.out.println(orderList);
			// 将查询到的所有的订单的结果交给PageHelper进行处理
			PageInfo<SysOrder> pageInfo = new PageInfo<SysOrder>(orderList);
			// 创建一个返回结果参数的实体类
			sysoOrderResult = new SysOrderResult();
			sysoOrderResult.setCode("0");
			sysoOrderResult.setMsg("");
			sysoOrderResult.setData(orderList);// 将查询的数据放到Result对象中去
			// 让paghHepler自动获取到查询订单数据的总条数
			sysoOrderResult.setCount(pageInfo.getTotal());
		} catch (Exception e) {
		}
		return sysoOrderResult;
	}

	/**
	 * 订单发货的方法
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateOrderOne.do")
	public SysOrderResult updateOrderByOrderIdOne(String id) {// 获取到前台传过来的数据
		System.out.println(id);
		System.out.println(id);
		System.out.println(id);
		System.out.println(id);
		System.out.println(id);
		System.out.println(id);
		Map<String, Object> elementsMap = new HashMap<String, Object>();
		elementsMap.put("order_id", id);
		elementsMap.put("order_state", "40");
		// 通过获取到的id进行更新
		int result = iorderService.updateOrderByOrderState(elementsMap);
		System.out.println(result);
		// 调用service层
		SysOrderResult sysoOrderResult = new SysOrderResult();
		sysoOrderResult.setCode("0");
		return sysoOrderResult;
	}

	/**
	 * 取消订单
	 * 
	 * @ResponseBody 规定传到前端界面的参数类型是json类型
	 * @RequestBody 规定从前端界面接受的参数类型是json
	 * @param orderId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateOrderTwo.do")
	public SysOrderResult updateOrderByOrderIdTwo(String id) {// 获取到前台传过来的数据
		Map<String, Object> elementsMap = new HashMap<String, Object>();
		elementsMap.put("order_id", id);
		elementsMap.put("order_state", "0");
		// 通过获取到的id进行更新
		int result = iorderService.updateOrderByOrderState(elementsMap);
		System.out.println(result);
		// 调用service层
		SysOrderResult sysoOrderResult = new SysOrderResult();
		sysoOrderResult.setCode("0");
		return sysoOrderResult;
	}

	/**
	 * 导出Excel @SuppressWarnings("unchecked") 作用：不让虚拟机进行提示强制类型转换可能异常
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/exportExcell.do")
	public void exportExcel(String orderSn, String userId, String userName, String orderState,
			HttpServletResponse response) {
		System.out.println("orderSn==========>" + orderSn + "userId=============>" + userId + "userName==============>"
				+ userName + "orderState==============>" + orderState);
		// 接收从前台传过来的值，并进行搜索，将其导出
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("order_sn", orderSn);
		parameterMap.put("user_id", userId);
		parameterMap.put("user_name", userName);
		parameterMap.put("order_state", orderState);
		// 使用数据进行查询
		List<SysOrder> orderList = iorderService.querryOrder(parameterMap);
		// 首先要判断数据是否为空
		if (!(orderList == null)) {
			try {
				// 创建导出的表格的表头
				String[] tableTitle = { "订单的编号", "用户ID", "用户的姓名" };
				List<Object[]> objectList = new ArrayList<Object[]>();
				for (SysOrder sysOrder : orderList) {
					Object[] obj = new Object[tableTitle.length];// 创建对应表头长度的数组用来装数据
					obj[0] = sysOrder.getOrderId();
					obj[1] = sysOrder.getUserId();
					obj[2] = sysOrder.getUserName();
					objectList.add(obj);
				}
				FileExport.exportExcel(tableTitle, objectList, "订单信息", response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 方式二
	 * @param data
	 * @param response
	 */
	@RequestMapping("/exportExcellNew.do")
	public void exportExcellNew(String data, HttpServletResponse response) {
		if (!(data == null)) {
			try {
				String[] tableTitle = { "订单序列号", "订单编号", "用户ID", "用户卡号", "订单添加的时间", "用户姓名" };
				// 创建最后的集合用来传输
				List<Object[]> objectList = new ArrayList<Object[]>();				
				List<SysOrder> dataList = JSONObject.parseArray(data, SysOrder.class);//将传输过来的数据转换格式对应的类的集合
				// 遍历集合里面的元素进行数据库的查询然后进行数据表格的打印
				for (SysOrder sysOrder : dataList) { // 创建数组，用来装表头对应的信息					
					Object[] obj = new Object[tableTitle.length];  
					obj[0] = sysOrder.getOrderId();
					obj[1] = sysOrder.getOrderSn();  
					obj[2] = sysOrder.getUserId(); 
					obj[3] = sysOrder.getUserCardNumber(); 
					obj[4] = sysOrder.getAddTime(); 
					obj[5] = sysOrder.getUserName();
					objectList.add(obj);
				}
				FileExport.exportExcel(tableTitle, objectList, "当前的页面的信息的Excel", response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
